<template>
  <div class="sentiment">
    <h1>当前股市舆评总体情感词云</h1>
    <h2>日期{{this.datenow}}</h2><br/>
    <el-button v-on:click="renew">更新</el-button><br/>
    <img :src=pic><br/>
    <el-button v-on:click="getPic">查询</el-button>
    <h1>当前股市情感指数</h1>
    <ul v-for="(senti, i) in sentis" :key="i">
      <li><h3>喜悦:{{senti.like}}</h3></li><br/>
      <li><h3>压恶:{{senti.hate}}</h3></li><br/>
      <li><h3>愤怒:{{senti.angry}}</h3></li><br/>
      <li><h3>低落:{{senti.upset}}</h3></li><br/>
    </ul>
  </div>
</template>

<script>
import axios from "axios"

export default {
  // eslint-disable-next-line vue/multi-word-component-names
  name: "sentiment",
  data() {
    return {
      sentis:[

      ],
      datenow: "2022-05-22",
      pic: null
    }
  },

  createded() {
    this.datenow = this.getCurrentDate();
    this.pic = "../assets/dummy1.png";
  },

  methods: {
    getCurrentDate: function (){
      var dd = new Date();
      var year = dd.getFullYear();
      var month =
          dd.getMonth() + 1 < 10 ? "0" + (dd.getMonth() + 1) : dd.getMonth() + 1;
      var day = dd.getDate() < 10 ? "0" + dd.getDate() : dd.getDate();
      return year + "-" + month + "-" + day;
    },

    getSenti: function () {
      axios({
        headers:{
          contentEncoding: "utf-8",
          'Content-Type': 'application/x-www-form-urlencoded',
          'Accept': 'application/json'
        },
        params: {"date": this.getCurrentDate()},
        method: "get",
        url: "http://localhost:8081/senti/senti"
      }).then((response) => {
            alert("the data is" + JSON.Stringify(response.data));
            this.sentis.push(response.data);
          }, (response) => {
            alert("content is" + JSON.Stringify(response.data));
            alert("no this senti!");
          });
    },

    getPic: function () {
      axios({
        method: "get",
        url:"http://localhost:8081/senti/sentiment",
        responseType: "blob"
      }).then( (response) => {
        this.sentis.push({
          like: 0.31,
          hate: 0.25,
          angry: 0.26,
          upset: 0.18
        });
        this.pic = window.URL.createObjectURL(response.data);
        return this.pic;
      }, (response) => {
        alert(JSON.stringify(response.data));
        alert("no pic!")
      })
    },

    renew: function () {
      axios({
        method: "get",
        url: "http://localhost:9080/crawl.json?spider_name=senti/"
      }).then((response) => {
        alert("renew success!");
      }, (response) => {
        alert("renew!");
      });
    }
  }
}
</script>
